package com.demo.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.demo.base.anno.CurrentUser;
import com.demo.base.anno.Params;
import com.demo.base.consant.AppConsant;
import com.demo.base.resp.ResufulResult;
import com.demo.base.user.UserInfoVO;

@RestController
public class CurrentUserController {

	@Resource
	private HttpSession httpSession;

	@RequestMapping("/register")
	public ResufulResult register(@Params(notEmpty = true, maxLength = 5, defaultValue = "我是默认值") String name,
			@Params(notEmpty = true, maxLength = 10) String passwd) {
		return ResufulResult.succeed(new UserInfoVO(name, passwd, "蓝精灵"));
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ResufulResult login(String userName, String passwd) {
		UserInfoVO userInfoVO = new UserInfoVO(userName, passwd, "蓝精灵");
		httpSession.setAttribute(AppConsant.CURRENT_USER, userInfoVO);
		return ResufulResult.succeed(userInfoVO);
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public ResufulResult logout() {
		httpSession.setAttribute(AppConsant.CURRENT_USER, null);
		httpSession.invalidate();
		return ResufulResult.succeed();
	}

	@RequestMapping(value = "/getUser", method = RequestMethod.GET)
	public ResufulResult getUser(@CurrentUser UserInfoVO userInfo) {
		return ResufulResult.succeed(userInfo);
	}
}
